package com.gitee.wsl.compose.draw.loading

import androidx.compose.animation.core.*
import androidx.compose.foundation.Canvas
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.blur
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.symbol.particle.ToPointParticle
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow

val ANGLES = 0..180
val RADIUS = 20..30

object FireLoading :Loading{
    @Composable
    override fun View(modifier: Modifier) {
        var width by remember { mutableStateOf(600f) }
        var height by remember { mutableStateOf(600f) }
        val centerx = width / 2
        val centery = height / 2
        val radius = width / 6
        val colorList = listOf(
            Color(0xffFF966B),
            Color(0xffFFBA23),
            Color(0xffF2C371),
            Color(0xffFFC000),
        )
        val transition = rememberInfiniteTransition(label = "transition")
        val angle by transition.animateFloat(
            0f, 360f, animationSpec = InfiniteRepeatableSpec(
                keyframes {
                    durationMillis = 2000
                    360f.at(2000)
                }
            ), label = "angle"
        )
        val tapx = pointX(radius, centerx, angle)
        val tapy = pointY(radius, centery, angle)
        val colorList2 = listOf(
            Color(0xff337AFF),
            Color(0xff01B9FF),
            Color(0xff00C1F2),
            Color(0xff3A69FF),
        )
        val tapx2 = pointX(radius, centerx, angle + 180f)
        val tapy2 = pointY(radius, centery, angle + 180f)

        val particleList = remember { mutableStateListOf<ToPointParticle>() }
        particleList.forEach {
            it.update()
        }

        LaunchedEffect(particleList.size) {
            flow {
                delay(10)
                emit(1)
            }.collect {
                val tempAngle = ANGLES.random()
                val tempRadius = RADIUS.random()
                val tempColor = colorList.random()
                val tempColor2 = colorList2.random()
                val ball = ToPointParticle(
                    startX = tapx, startY = tapy,
                    angle = tempAngle,
                    dis = 2f,
                    size = tempRadius.toFloat(),
                    color = tempColor
                )
                val ball2 = ToPointParticle(
                    startX = tapx2, startY = tapy2,
                    angle = tempAngle,
                    dis = 2f,
                    size = tempRadius.toFloat(),
                    color = tempColor2
                )
                particleList.add(ball2)
                particleList.add(ball)
            }
        }


        Canvas(modifier = modifier.blur(7.dp)) {
            width = size.width
            height = size.height
            particleList.forEach {
                drawCircle(
                    color = it.color,
                    radius = it.size,
                    center = Offset(it.endx, it.endy),
                    alpha = it.alpha
                )
            }
        }
    }

}

@Composable
fun Loading.Companion.Fire(modifier: Modifier) = FireLoading(modifier)

val Loading.Companion.Fire get() = FireLoading
